package cn.south.campus.admin.campus.service.impl;

import cn.south.campus.admin.campus.mapper.ComplaintMapper;
import cn.south.campus.admin.campus.model.entity.Complaint;
import cn.south.campus.admin.campus.model.enums.ComplaintEnums;
import cn.south.campus.admin.campus.model.req.ComplaintReq;
import cn.south.campus.admin.campus.model.vo.ComplaintVo;
import cn.south.campus.admin.campus.service.CommentService;
import cn.south.campus.admin.campus.service.ComplaintCountService;
import cn.south.campus.admin.campus.service.ComplaintService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 校园吐槽表 服务实现类
 * </p>
 *
 * @author chow
 * @since 2024-12-05
 */
@Service
@AllArgsConstructor
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements ComplaintService {

    private final ComplaintMapper complaintMapper;

    private final ComplaintCountService complaintCountService;

    private final CommentService commentService;

    @Override
    public IPage<ComplaintVo> pageQuery(ComplaintReq query) {
        query.setStatus(ComplaintEnums.PUBLISH.getCode());
        PageDTO<Complaint> complaintPageDTO = complaintMapper.pageQuery(query.getPageDto(), query);
        List<Long> complaintIds = complaintPageDTO.getRecords().stream().map(Complaint::getId).collect(Collectors.toList());
        Map<Long, boolean[]> res = complaintCountService.selectMeOpt(complaintIds, ComplaintEnums.BizType.COMPLAINT.getCode());
        Map<Long, Map<Integer, Long>> cntMap = complaintCountService.selectCount(complaintIds, ComplaintEnums.BizType.COMPLAINT.getCode());
        Map<Long, Long> commentCntMap = commentService.selectCount(complaintIds);
        return complaintPageDTO.convert(item -> {
            ComplaintVo complaintVo = ComplaintVo.convert(item);
            if (!CollectionUtils.isEmpty(res) && res.get(item.getId()) != null) {
                complaintVo.setAgree(res.get(item.getId())[0]);
                complaintVo.setReject(res.get(item.getId())[1]);
            }
            if (!CollectionUtils.isEmpty(commentCntMap)) {
                complaintVo.setCommentCount(commentCntMap.getOrDefault(item.getId(), 0L));
            }
            if (!CollectionUtils.isEmpty(cntMap) && !CollectionUtils.isEmpty(cntMap.get(item.getId()))) {
                Map<Integer, Long> longMap = cntMap.get(item.getId());
                complaintVo.setAgreeCount(longMap.getOrDefault(ComplaintEnums.CountType.LIKE.getCode(), 0L));
                complaintVo.setRejectCount(longMap.getOrDefault(ComplaintEnums.CountType.DISLIKE.getCode(), 0L));
            }
            return complaintVo;
        });
    }

    @Override
    public ComplaintVo detail(Long id) {
        Complaint complaint = complaintMapper.detail(id, ComplaintEnums.PUBLISH.getCode());
        ComplaintVo complaintVo = ComplaintVo.convert(complaint);
        Map<Integer, Long> longMap = complaintCountService.selectCount(complaintVo.getId(), ComplaintEnums.BizType.COMPLAINT.getCode());
        boolean[] booleans = complaintCountService.selectMeOpt(complaintVo.getId(), ComplaintEnums.BizType.COMPLAINT.getCode());
        complaintVo.setAgree(booleans[0]);
        complaintVo.setReject(booleans[1]);
        if (!CollectionUtils.isEmpty(longMap)) {
            complaintVo.setAgreeCount(longMap.getOrDefault(ComplaintEnums.CountType.LIKE.getCode(), 0L));
            complaintVo.setRejectCount(longMap.getOrDefault(ComplaintEnums.CountType.DISLIKE.getCode(), 0L));
        }
        return complaintVo;
    }


    @Override
    public void updateStatusComplaint(Long id, Integer status) {
        Complaint complaint = complaintMapper.selectById(id);
        complaint.setStatus(status);
        complaintMapper.updateById(complaint);
    }
}
